package com.bf.xpdd.persistent.sqlmapdao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bf.xpdd.domain.ActivityGlobal;
import com.bf.xpdd.persistent.iface.DaoActivityGlobal;
import com.ibatis.sqlmap.client.SqlMapClient;

public class DaoActivityGlobalImpl implements DaoActivityGlobal
{
	/**
	 * 日志记录
	 */
	private Logger logger = Logger.getLogger(DaoActivityGlobalImpl.class);

	/**
	 * ibatis的查询接口
	 */
	private SqlMapClient sqlMapClient;

	/**
	 * 注入sqlMapClient
	 * 
	 * @param sqlMapClient
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	@Override
	public void addActivityGlobal(ActivityGlobal global)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.insert("activityGlobal.addActivityGlobal", global);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityGlobal(ActivityGlobal global)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activityGlobal.modifyActivityGlobal", global);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityStatus(long id, String nick, int status)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("nick", nick);
		map.put("status", status);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activityGlobal.modifyActivityStatus", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void delActivityGlobal(long id)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activityGlobal.delActivityGlobal", id);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public ActivityGlobal queryGlobalByNick(String nick)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			Object obj = this.sqlMapClient.queryForObject("activityGlobal.queryGlobalByNick", nick);
			this.sqlMapClient.commitTransaction();
			return (ActivityGlobal) obj;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

}
